package com.zhangyu.shadow.repair.delegate.repair;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.config.RepairItemType;
import com.zhangyu.shadow.repair.config.TroubleInfo;
import com.zhangyu.shadow.repair.delegate.repair.entity.Level0Item;
import com.zhangyu.shadow.repair.delegate.repair.entity.Level1Item;
import com.zhangyu.shadow.repair.delegate.search.AddTroubleDelegate;
import com.zhangyu.shadow.repair.delegate.search.entity.ProcessEntity;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import st.lib.base.delegate.RxShadowDelegate;
import st.lib.base.ui.recycler.MultipleField;
import st.lib.base.ui.recycler.MultipleItemEntity;

/**
 * Created by zhangyu on 2016/8/9.
 */
public class ExpandableItemAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private LayoutInflater mInflater;
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    private RxShadowDelegate mRepairDelegate;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ExpandableItemAdapter(RxShadowDelegate repairDelegate, List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.item_trouble_head);
        addItemType(TYPE_LEVEL_1, R.layout.item_trouble_content);
        mInflater = LayoutInflater.from(repairDelegate.getContext());
        mRepairDelegate = repairDelegate;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final MultiItemEntity item) {
        switch (holder.getItemViewType()) {
            case TYPE_LEVEL_0:
                final Level0Item lv0 = (Level0Item) item;
                holder.setText(R.id.tv_name, lv0.mName);
                if (lv0.isCanAdd) {
                    holder.getView(R.id.card_factory).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int position = holder.getAdapterPosition();
                            int type = 0;
                            if (mRepairDelegate instanceof RepairDelegate) {
                                type = 1;
                            }
                            TroubleInfo.setInfos(
                                    (ArrayList<MultipleItemEntity>) lv0.getSubItem(0).mMultiItemEntities);
                            mRepairDelegate.start(AddTroubleDelegate.create(position, lv0.mName,
                                    type, lv0.mCode));
                        }
                    });
                } else {
                    holder.setVisible(R.id.tv_add, false);
                }
                break;
            case TYPE_LEVEL_1:
                final Level1Item lv1 = (Level1Item) item;
                TagFlowLayout tagFlowLayout = holder.getView(R.id.tag_flow_layout);
                final TagAdapter<MultipleItemEntity> tagAdapter =
                        new TagAdapter<MultipleItemEntity>(lv1.mMultiItemEntities) {
                            @Override
                            public View getView(FlowLayout parent, final int position,
                                                MultipleItemEntity multipleItemEntity) {
                                View view;
                                if (RepairItemType.ITEM_FINISH_PLAN == multipleItemEntity.getItemType()) {
                                    view = mInflater.inflate(R.layout.item_trouble_content_tag,
                                            parent, false);
                                    TextView tv = view.findViewById(R.id.tv_content);
                                    tv.setText((CharSequence) multipleItemEntity.getField(MultipleField.NAME));
                                } else {
                                    view = mInflater.inflate(R.layout.item_trouble_with_process,
                                            parent, false);
                                    TextView tvTrouble = view.findViewById(R.id.tv_trouble_name);
                                    tvTrouble.setText((CharSequence) multipleItemEntity.getField(MultipleField.NAME));
                                    List<ProcessEntity> processBeans
                                            = multipleItemEntity.getField(RepairItemType.ITEM_LIST_PROCESS);
                                    if (processBeans != null) {
                                        StringBuilder content = new StringBuilder();
                                        int i = 1;
                                        for (ProcessEntity bean
                                                : processBeans) {
                                            content.append("(")
                                                    .append(i++)
                                                    .append(")")
                                                    .append(bean.getOperationName())
                                                    .append(bean.getUsedCount())
                                                    .append(bean.getUnit())
                                                    .append(bean.getComponentOrAccessoryName())
                                                    .append("\n");
                                        }
                                        TextView tvProcess = view.findViewById(R.id.tv_trouble_process);
                                        tvProcess.setText(content.toString());
                                    }
                                }
                                return view;
                            }
                        };
                tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                    @Override
                    public boolean onTagClick(View view, int position, FlowLayout parent) {
                        if (RepairItemType.ITEM_FINISH_PLAN ==
                                lv1.mMultiItemEntities.get(position).getItemType()) {
                            lv1.mMultiItemEntities.remove(position);
                            tagAdapter.notifyDataChanged();
                            int p = holder.getAdapterPosition();
                            notifyItemChanged(p - 1);
                        }
                        return false;
                    }
                });
                tagFlowLayout.setAdapter(tagAdapter);
                break;
            default:
                break;
        }
    }
}
